package org.m3;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * Get Started!
 * @author yakovlev.yuriy@gmail.com
 * @author Dmitry Makeev
 */
public class Tutorial extends Activity implements OnClickListener {
    private int index = 0;
    private List<Animals> animalsList;
    private TextView name;
    private ImageView image;
    private MediaPlayer mp;
    private Button btnHome;
    private Button btnSpeaker;
    private Button btnNext;
    
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        L10N.init(this.getResources());
        setContentView(R.layout.tutorial);

        initBtnHome();
        initBtnSpeaker();
        initBtnNext();

        animalsList = Arrays.asList(Animals.values());
        Collections.shuffle(animalsList);
        
        nextAnimal();
	}

	@Override
    public void onClick(View v) {
    	if(mp!=null) {
    		mp.release();
    	}
    	if(v.equals(getImage())) {
    		playSound();
    	} else if(v.equals(btnHome)) {
    		finish();
    	}  else if(v.equals(btnSpeaker)) {
    		Home.soundEnabled = !Home.soundEnabled;
    		btnSpeaker.setCompoundDrawablesWithIntrinsicBounds(null, Tutorial.this.getResources().getDrawable(Home.soundEnabled ? R.drawable.speaker : R.drawable.dspeaker), null, null);
    	}  else if(v.equals(btnNext)) {
    		if(++index >= Animals.values().length) index = 0;
        	nextAnimal();
    	}
    }
    
	
	// PRIVATE METHODS
	
	private void initBtnHome() {
    	if(btnHome == null) {
    		btnHome = (Button) findViewById(R.id.btnHome);
    		btnHome.setOnClickListener(this);
    	}
    }

    private void initBtnSpeaker() {
    	if(btnSpeaker == null) {
    		btnSpeaker = (Button) findViewById(R.id.btnSpeaker);
    		btnSpeaker.setOnClickListener(this);
    		btnSpeaker.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(Home.soundEnabled ? R.drawable.speaker : R.drawable.dspeaker), null, null);
    	}
    }

    private void initBtnNext() {
    	if(btnNext == null) {
    		btnNext = (Button) findViewById(R.id.btnNext);
    		btnNext.setOnClickListener(this);
    	}
    }

    private TextView getName() {
    	if(name == null) {
    		name = (TextView) findViewById(R.id.name);
    		name.setTextAppearance(this, R.style.animalName);
            name.setTypeface(L10N.font);
        }
    	return name;
    }
    
    private ImageView getImage() {
    	if(image == null) {
    		image = (ImageView) findViewById(R.id.image);
    		image.setOnClickListener(this);
        }
    	return image;
    }
    
	private void nextAnimal() {
        getName().setText(this.getResources().getString(animalsList.get(index).getName()));
        getName().setTextColor(this.getResources().getColor(animalsList.get(index).getColor()));
        
        getImage().setImageDrawable(Tutorial.this.getResources().getDrawable(animalsList.get(index).getImage()));
        //Display display = ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
        //int height = display.getHeight() / 3 - 80;
        //int width = display.getWidth() / 3 - 80;
        //image.setPadding(0, display.getOrientation()==0 ? height : width, 0, 0);
        
        if(mp!=null) {
        	mp.release();
        }
    	playSound();
	}
	
	private void playSound() {
		if(Home.soundEnabled) {
			try {
		        mp = MediaPlayer.create(Tutorial.this, animalsList.get(index).getSound());
	    		if(mp!=null) {
					mp.start();
	    		}
	        }catch(Exception e) {
	        	Log.e(Tutorial.class.getName(), e.toString());
            }
		}
    }
	
}
